
// 登录接口
import { login } from '@/api/loginApi';
// 信息提示
import msgInfo from '@/utils/msgInfo';
export default ( { fromRef } ) => {
  // 获取dom
  let ruleFormRef = ref( fromRef );

  // 数据
  const ruleForm = reactive( {
    username: 'admin',
    password: '123456'

  } )

  // 路由
  let router = useRouter();
  // vuex
  let store = useStore();

  // 登录点击事件
  const submitForm = async ( formEl ) => {
    if ( !formEl ) return
    await formEl.validate( async ( valid, fields ) => {
      if ( valid ) {
        let { data, meta } = await login( ruleForm );
        if ( !msgInfo( meta ) ) return;
        router.push( '/home' );
        store.commit( 'SAVE_TOKEN', data.token );
      } else {
        console.log( 'error submit!', fields )
      }
    } )
  }

  return {
    submitForm, ruleFormRef, ruleForm
  };
}